package com.cssnj.portal.service.user;

import com.cssnj.portal.domain.user.GlGnSyqkEntity;
import com.cssnj.portal.domain.user.GlGnSyqkRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.common.TreeNode;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.domain.exception.BaseSysException;
import io.predictech.framework.service.base.BaseService;
import io.predictech.framework.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * TODO
 *
 * @author Arthur
 * @version 1.0
 * @since 2018/8/20 10:50
 */
@Service("userService")
public class UserService extends BaseService {

    @Autowired
    private GlGnSyqkRepository glGnSyqkRepository;

    @Override
    protected JsonResponse invokingService(JsonRequest jsonRequest) throws SQLException, BaseException {
        if ("saveGnDjs".equals(jsonRequest.getHandleCode())) {
            return saveGnDjs(jsonRequest);
        } else if ("queryGnDjLb".equals(jsonRequest.getHandleCode())) {
            return queryGnDjLb(jsonRequest);
        }
        return null;
    }

    //记录功能点击数
    @Transactional
    public JsonResponse saveGnDjs(JsonRequest jsonRequest) throws SQLException {
        JsonResponse jsonResponse = new JsonResponse();
        String gnid = (String) jsonRequest.getReqData().get("gnid");
        String swrydm = (String) jsonRequest.getReqData().get("swrydm");
        String swjgdm = (String) jsonRequest.getReqData().get("swjgdm");
        String swrysfdm = (String) jsonRequest.getReqData().get("swrysfdm");
        String uuid = UUID.randomUUID().toString();
        Timestamp lrsj = this.queryDao.getSystimestamp();
        GlGnSyqkEntity vo = new GlGnSyqkEntity();
        vo.setUuid(uuid);
        vo.setSwryDm(swrydm);
        vo.setSwjgDm(swjgdm);
        vo.setSwrysfDm(swrysfdm);
        vo.setGnId(gnid);
        vo.setLrSj(lrsj);
        glGnSyqkRepository.save(vo);
        return jsonResponse;
    }

    //查询功能点击记录(分页查询)
    private JsonResponse queryGnDjLb(JsonRequest jsonRequest) throws SQLException, BaseException {
        SqlRowSet rs;
        JsonResponse jsonResponse = new JsonResponse();
        String sql = "";
        String total_sql = "";
        //获取请求参数
        String swrysfdm = (String) jsonRequest.getReqData().get("swrysfdm");
        String cxrqq = (String) jsonRequest.getReqData().get("cxrqq");
        String cxrqz = (String) jsonRequest.getReqData().get("cxrqz");
        String rows = (String) jsonRequest.getReqData().get("rows");
        String page = (String) jsonRequest.getReqData().get("page");
        if (null == rows || "".equals(rows)) {
            rows = "10";
        }
        if (null == page || "".equals(page)) {
            page = "1";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c;
        if (null == cxrqq) {
            //没有取当前日期-30天
            c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -30);
            cxrqq = format.format(c.getTime());
        }
        if (null == cxrqz) {
            //没有取当前日期
            c = Calendar.getInstance();
            cxrqz = format.format(c.getTime());
        }
        //查询语句
        sql = "select hc, gn_id, gn_mc, href, iconcls, open_type, sjgn_id, xt_dm, sj from (select rownum hc, gn_id, gn_mc, href, iconcls, open_type, sjgn_id, xt_dm, sj from (select b.gn_id, b.gn_mc, b.href, decode(b.open_type, '0', 'fa-folder', nvl(b.jdys, 'fa-file')) iconcls, b.open_type, b.sjgn_id, b.xt_dm, to_char(a.lr_sj, 'yyyy-mm-dd hh24:mi:ss') sj from gl_gn_syqk a, gl_gn b where a.swrysf_dm = ? and a.lr_sj >= to_date(? || ' 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and a.lr_sj <= to_date(? || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss') and a.gn_id = b.gn_id and b.xy_bj = '1' and b.xs_bj = '1' order by a.lr_sj desc)) d where hc >= ? * (? -1) + 1 and hc <= ? * ?";
        total_sql = "select count(1) as gs from gl_gn_syqk a, gl_gn b where a.swrysf_dm = ? and a.lr_sj >= to_date(? || ' 00:00:00', 'yyyy-mm-dd hh24:mi:ss') and a.lr_sj <= to_date(? || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss') and a.gn_id = b.gn_id and b.xy_bj='1' and b.xs_bj='1'";
        //查询参数
        List<Object> params = new ArrayList<Object>();
        params.add(swrysfdm);
        params.add(cxrqq);
        params.add(cxrqz);
        params.add(rows);
        params.add(page);
        params.add(rows);
        params.add(page);
        List<Object> total_params = new ArrayList<Object>();
        total_params.add(swrysfdm);
        total_params.add(cxrqq);
        total_params.add(cxrqz);
        rs = this.queryDao.queryForRowSet(sql, params);
        Map<String, Object> map;
        List<Map<String, Object>> jlList = new ArrayList<Map<String, Object>>();
        while (rs.next()) {
            map = new HashMap<String, Object>();
            map.put("hc", rs.getString("hc"));
            map.put(TreeNode.ID.getKey(), rs.getString("gn_id"));
            map.put(TreeNode.TEXT.getKey(), rs.getString("gn_mc"));
            map.put(TreeNode.HREF.getKey(), rs.getString("href"));
            map.put(TreeNode.ICONCLS.getKey(), rs.getString("iconcls"));
            map.put(TreeNode.MODE.getKey(), rs.getString("open_type"));
            map.put(TreeNode.PID.getKey(), rs.getString("sjgn_id"));
            map.put(TreeNode.SYSCODE.getKey(), rs.getString("xt_dm"));
            map.put("sj", rs.getString("sj"));
            jlList.add(map);
        }
        map = this.queryDao.queryForMap(total_sql, total_params);
        String total = String.valueOf(map.get("gs"));
        try {
            ObjectMapper objectMapper = StringUtil.getBaseObjectMapper();
            HashMap<String, Object> r_map = new HashMap<String, Object>();
            r_map.put("total", total);
            r_map.put("rows", jlList);
            jsonResponse.getRepData().put("str", objectMapper.writeValueAsString(r_map));
        } catch (JsonProcessingException e) {
            throw new BaseSysException(e.getMessage());
        }
        return jsonResponse;
    }
}
